<?php
namespace app\controller;

use app\BaseController;
use app\utils\data\ele\TableV2Columns;
use app\utils\script\ele\feedback\Message;
use app\utils\script\ele\feedback\UploadSuccess;
use app\utils\script\JumpHrefScript;
use app\utils\script\PostScript;
use app\utils\ui\ele\AutoResizer;
use app\utils\ui\ele\Button;
use app\utils\ui\ele\Radio;
use app\utils\ui\ele\RadioGroup;
use app\utils\ui\native_element\Root;
use app\utils\ui\native_element\Text;
use app\utils\ui\native_element\Element;
use app\utils\ui\ele\EleScript;
use app\utils\ui\ele\Form;
use app\utils\ui\ele\FormItem;
use app\utils\ui\ele\Input;
use app\utils\ui\ele\Pagination;
use app\utils\ui\ele\Table;
use app\utils\ui\ele\TableColumn;
use app\utils\ui\ele\TableV2;
use app\utils\ui\ele\Upload;
use app\utils\ui\vue\SampleEleVue;
use app\utils\ui\vue\VueMethod;
use app\utils\ui\vue\VueVarValue;
use Exception;
use think\facade\Db;
use think\file\UploadedFile;

class Index extends BaseController
{
    
    public function test(){
        // $root = new Root(
        //     new Element('div',true,[
        //         'id'=>'app'
        //     ],
        //         new VueList(
        //             [
        //                 new Row([],
        //                     new Input('input_1','',[
        //                         Input::attr_placeholder=>'请输入你要填的东西'
        //                     ])
        //                 ),
        //                 new RadioGroup('test2','A',
        //                 [
        //                     RadioGroup::event_change=>VueMethod::method('test2_change')
        //                 ],[
        //                     new Radio(null,null,[
        //                         'label'=>'A'
        //                     ],new Text('A')),
        //                     new Radio(null,null,[
        //                         'label'=>'B'
        //                     ],new Text('B'))
        //                 ]),
        //                 new Text('welcome to text'),
        //                 new Button(
        //                     [
        //                         'icon'=>Icon::Platform
        //                     ],
        //                     [
        //                         // new Icon('Search'),
        //                         new Text('请点我')
        //                     ]
        //                 ),

        //                 new Col(
        //                     [],
        //                     new Text('COL1')
        //                 ),
        //                 new Row(
        //                     [],
        //                     [
        //                         new Col([
        //                             Col::attr_span=>'12'
        //                         ],new Text('COL2-1')),
        //                         new Col([
        //                             Col::attr_span=>'12'
        //                         ],new Text('COL2-2')),
        //                         new Col([
        //                             Col::attr_span=>'12'
        //                         ],new Text('COL2-3')),
        //                     ]
        //                 ),
        //                 new VueText('title','充值'),
        //                 new Radio(
        //                     'check',
        //                     'abc',
        //                     [],
        //                     new Text(
        //                         '你好1'
        //                     )
        //                 ),
        //                 new Radio(
        //                     'check2',
        //                     'abc',
        //                     [],
        //                     new Text(
        //                         '你好2'
        //                     )
        //                 ),
        //                 new Link([
        //                     ':'.Link::attr_href=>VueVarValue::var('test','http://www.baidu.com')
        //                 ],new Text('这是一个链接'))
        //             ]
        //         )
        //     )
        // );


        $root = new Root(
            new Element('div',true,[
                'id'=>'app'
            ],
                new Form([
                    'label-width'=>"120px"
                ],[
                    new FormItem('名字',[],
                        new Input('user_name','',[])
                    ),
                    new FormItem('性别',[],
                    new RadioGroup('sex','boy',[],
                        [
                            new Radio('','',[
                                'label'=>'boy',
                            ],new Text('男')),
                            new Radio('','',[
                                'label'=>'girl',
                            ],new Text('女'))
                        ]
                    )),
                    new FormItem('手机号码',[],
                        new Input('phone','',[])
                    ),
                    new FormItem('邮箱',[],
                        new Input('email','',[])
                    ),
                    new FormItem('',[],
                        [
                            new Button([
                                'type'=>'primary',
                                'click'=>VueMethod::method('submit')
                            ],new Text('提 交')),
                            new Button([],new Text('重 置'))
                        ]
                    ),
                    
                ])
            )
        );

        $html = $root->render();
        $html.= $root->script(new EleScript('#app',$root->data(),$root->method(),[
            // 'test2_change'=>'alert(1);'
            'submit'=>new PostScript('POST','/Index/test2')
        ]));
        return response($html);
        // dump();
        // dump($root->data());
        // dump($root->method());
    }

    public function coupons_list(){
        
        $page=$this->request->get('page','1');
        $keyword = $this->request->get('sellername','');

        $total = Db::name('coupons')->where(array(
            array('seller_name','like','%'.$keyword.'%')
        ))->count();
        $list = Db::name('coupons')->where(array(
            array('seller_name','like','%'.$keyword.'%')
        ))->page(intval($page),10)->order('id','DESC')->select()->toArray();
        $field = [];
        if(count($list)>=1){
            foreach($list[0] as $k=>$v){
                $field[]=new TableColumn($k,$k);
            }
        }
        $root = new Root( new Element('div',true,[
            'id'=>'app'
        ],[
            new Form([
                Form::inline=>'true'
            ],
            [
                new FormItem('搜商家名称',[],new Input('sellername',$this->request->get('sellername',''),[])),
                new FormItem('',[],
                new Button([
                    'click'=>new VueMethod('search')
                ],new Text('搜索')))
            ]),
            new Table([
                Table::attr_border=>'',
                Table::attr_data=>VueVarValue::var('list',$list)
            ],$field),
            new Pagination('page_change',$this->request->get('page',1),$total,10)
        ]));
        
        $html = $root->render(false);
        $html.= $root->script(new EleScript('#app',$root->data(),$root->method(),[
            // 'test2_change'=>'alert(1);'
            // 'submit'=>new PostScript('POST','/Index/test2')
            'search'=>new JumpHrefScript('/index/coupons_list',['list']),
            'page_change'=>new JumpHrefScript('/index/coupons_list',['list'],null,[
                'page'=>new VueVarValue('val','1')
            ],'val')
        ]));
        return response($html);
    }

    public function test2(){
        
        return (new SampleEleVue([

            new Upload('file',[],
            [
                new Text('点击上传图片')
            ],[
                'action'=>'?s=index/upload',
                ':on-success'=>VueMethod::method('uploadsuccess',true)
            ]),

            new AutoResizer(
                'width:600px;height:700px',
                new TableV2([
                    TableV2::attr_data=>VueVarValue::var('data',[
                        [
                            'name'=>'hello this is data'
                        ]
                    ]),
                    TableV2::attr_columns=>(new TableV2Columns('columns'))->add_column('name','name',150)
                ])
            )
        ]))->response([
            'a'=>1
        ],[
            'uploadsuccess'=>new UploadSuccess(new Message('成功'))
        ]);
    }

    public function test3(...$prop){
        dump($prop);
    }

    public function upload(){
        
        throw new Exception('test');
        /**
         * @var UploadedFile
         */
        $file = $this->request->file('file');
        $file->move('upload','abc.jpg');
        dump($file);
    }

}
